# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=scudo-malloc
# tied to llvm version
pkgver=19.1.2
pkgrel=0
pkgdesc="Standalone scudo malloc from compiler-rt"
url="https://llvm.org"
arch="all"
license="Apache-2.0 WITH LLVM-exception"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="linux-headers"
checkdepends="gtest-dev"
subpackages="$pkgname-static $pkgname-dev"
source="https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver//_/-}/compiler-rt-${pkgver//_/}.src.tar.xz
	0001-Add-makefile.patch
	"
builddir="$srcdir/compiler-rt-${pkgver//_/}.src/lib/scudo/standalone"

case "$CARCH" in
ppc64le)
	# tests/common_test.cpp:33: Failure
	# Expected: (OnStart) > (0UL), actual: 0 vs 0
	# https://gitlab.alpinelinux.org/alpine/aports/-/issues/16235
	options="!check"
	;;
esac

build() {
	local cflags_crc32=
	unset CXXFLAGS
	case "$CARCH" in
		aarch64)
			# this has broken emulation in aarch64 qemu (incl qemu-user),
			# which causes it to abort instantly
			CXXFLAGS="-DSCUDO_DISABLE_TBI"
			cflags_crc32="-march=armv8-a+crc"
			;;
		x86*)	cflags_crc32="-mcrc32"
			;;
	esac

	msg "building libscudo"
	make CXXFLAGS="$CXXFLAGS" CXXFLAGS_crc32="$cflags_crc32" TESTS="$(want_check && echo yes || echo no)"
}

check() {
	make test
}

package() {
	install -Dm755 libscudo.so -t "$pkgdir"/usr/lib/
	install -Dm644 libscudo.a -t "$pkgdir"/usr/lib/
	install -Dm644 include/scudo/interface.h -t "$pkgdir"/usr/include/scudo/
}

sha512sums="
ae8e649abd6d5e0347d827f551102093995b94e2107868700a23fad7ebc8a727741a62630aa9aea8774805e66b14ea124399785b859a182c2de3c46f10e2f038  compiler-rt-19.1.2.src.tar.xz
1102a512adde2ef31aafa6011a4293d5fa361aa920f88ac20599aab84711e973315e6f6719631be11007aea869ae700e34ed57c7dc7724e8aacbc5b493632849  0001-Add-makefile.patch
"
